package zym.diandian.tools;


import android.annotation.SuppressLint;
import android.os.Build;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.regex.Pattern;

//CPU
public class CpuHelper {

    public static String[] getCpuInfo() throws IOException {
        String cpu[] = new String[3];
        //		cpu[0] = CpuHelper.getProcessor();
        //		cpu[1] = "" + CpuHelper.getNumCores();
        //		cpu[2] = "" + CpuHelper.getMinCpuFreq() + "~" + CpuHelper.getMaxCpuFreq();
        return cpu;
    }

    /**
     * 实时获取CPU当前频率（单位KHZ）
     *
     * @return
     */
    public static String getCurCpuFreq() {
        String result = "UNKNOWN";
        try {
            FileReader fr = new FileReader("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq");
            BufferedReader br = new BufferedReader(fr);
            String text = br.readLine();
            result = (Integer.parseInt(text.trim()) / 1000) + " MHZ";
            br.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 获取CPU最大频率
     *
     * @return
     */
    public static String getMaxCpuFreq() {
        String result = "";
        ProcessBuilder cmd;
        try {
            String[] args = {"/system/bin/cat", "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq"};
            cmd = new ProcessBuilder(args);
            Process process = cmd.start();
            InputStream in = process.getInputStream();
            result = (Integer.parseInt(NetUtil.inputStream2String(in).trim()) / 1000) + " MHZ";
            in.close();
        } catch (IOException ex) {
            ex.printStackTrace();
            result = "UNKNOWN";
        }
        return result.trim();
    }

    /**
     * 获取CPU最小频率
     *
     * @return
     */
    public static String getMinCpuFreq() {
        String result = "UNKNOWN";
        ProcessBuilder cmd;
        try {
            String[] args = {"/system/bin/cat", "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq"};
            cmd = new ProcessBuilder(args);
            Process process = cmd.start();
            InputStream in = process.getInputStream();
            result = (Integer.parseInt(NetUtil.inputStream2String(in).trim()) / 1000) + "MHZ";
            in.close();
            process.destroy();

        } catch (IOException ex) {
            ex.printStackTrace();
            result = "UNKNOWN";
        }
        return result.trim();
    }

    /**
     * 获取CPU核心数
     *
     * @return
     */
    public static int getNumCores() {
        // Private Class to display only CPU devices in the directory listing
        class CpuFilter implements FileFilter {

            @Override
            public boolean accept(File pathname) {
                // Check if filename is "cpu", followed by a single digit number
                if (Pattern.matches("cpu[0-9]", pathname.getName())) {
                    return true;
                }
                return false;
            }
        }

        try {
            // Get directory containing CPU info
            File dir = new File("/sys/devices/system/cpu/");
            // Filter to only list the devices we care about
            File[] files = dir.listFiles(new CpuFilter());
            // Return the number of cores (virtual CPU devices)
            return files.length;
        } catch (Exception e) {
            // Print exception
            e.printStackTrace();
            // Default to return 1 core
            return 1;
        }
    }

    /**
     * 获取处理器
     *
     * @return
     * @throws IOException
     */
    public static String getProcessor() {
        String processor = "UNKNOWN";
        try {
            Process process = Runtime.getRuntime().exec("cat /proc/cpuinfo");
            InputStream iStream = process.getInputStream();
            InputStreamReader iReader = new InputStreamReader(iStream);
            BufferedReader bReader = new BufferedReader(iReader);
            String buff = "";
            while ((buff = bReader.readLine()) != null) {
                if (buff.startsWith("Processor")) {
                    processor = buff.split(":")[1];
                    break;
                }
            }
        } catch (Exception e) {
            processor = "UNKNOWN";
        }
        return processor;
    }

    /**
     * 获取处理器类型
     */
    @SuppressLint("NewApi")
    public static String getProcessBit() {
        String bit = null;
        try {
            if (Build.VERSION.SDK_INT >= 21) {
                String[] bits = android.os.Build.SUPPORTED_64_BIT_ABIS;
                if (bits.length > 0) {
                    bit = "64-bit";
                } else {
                    bit = "32-bit";
                }
            } else {
                bit = "32-bit";
            }
        } catch (Exception e) {
            bit = "32-bit";
        }

        return bit;
    }
}
